<template>
    <div class="app-container">
<!--        <el-form :inline="true" :model="queryParams" label-width="68px" ref="queryForm"-->
<!--                 style="text-align: left">-->
<!--            <el-form-item label="选择日期范围查询">-->
<!--                <el-date-picker-->
<!--                        v-model="queryParams"-->
<!--                        type="datetimerange"-->
<!--                        range-separator="至"-->
<!--                        start-placeholder="起始时间"-->
<!--                        end-placeholder="结束时间"-->
<!--                        @change="selectTime"-->
<!--                />-->
<!--            </el-form-item>-->
<!--            <el-form-item>-->
<!--                <el-button @click="handleQuery" type="warning"><el-icon><Search /></el-icon>搜索</el-button>-->
<!--                <el-button @click="resetQuery"><el-icon><Refresh /></el-icon>重置</el-button>-->
<!--            </el-form-item>-->
<!--        </el-form>-->

        <el-row :gutter="10" style="margin: 10px 0px">
            <el-col :span="1.5">
                <el-button
                        type="primary"
                        plain
                        @click="addButton()"
                ><el-icon><EditPen /></el-icon>写日志</el-button>
            </el-col>
        </el-row>

        <el-card class="box-card">
            <template #header>
                <div>
                    <span>日志管理中心</span>
                    <el-button text>Physical Examination Appointment Records</el-button>
                </div>
            </template>
            <el-table :data="list" row-key="id" v-loading="loading">
                <el-table-column align="center" type="index" width="50"/>
                <el-table-column align="center" label="提交时间" prop="time">
                    <template #default="scope">
                        <span>{{ $filters.formatDate(scope.row.time) }}</span>
                    </template>
                </el-table-column>
                <el-table-column align="center" label="身高/cm" prop="height"/>
                <el-table-column align="center" label="体重/斤" prop="weight"/>
                <el-table-column align="center" label="饮食" :show-overflow-tooltip="true" prop="diet"/>
                <el-table-column align="center" label="心情" prop="mood">
                    <template #default="scope">
                        <el-tag class="ml-2" type="warning" v-if="scope.row.mood==='L'">美丽至极</el-tag>
                        <el-tag class="ml-2" type="warning" v-if="scope.row.mood==='M'">还不错</el-tag>
                        <el-tag class="ml-2" type="warning" v-if="scope.row.mood==='S'">emo~了</el-tag>
                    </template>
                </el-table-column>
<!--                <el-table-column align="center" label="睡眠" prop="sleep">-->
<!--                    <template #default="scope">-->
<!--                        <el-tag class="ml-2" type="warning" v-if="scope.row.sleep==='L'">极好</el-tag>-->
<!--                        <el-tag class="ml-2" type="warning" v-if="scope.row.sleep==='M'">一般</el-tag>-->
<!--                        <el-tag class="ml-2" type="warning" v-if="scope.row.sleep==='S'">失眠</el-tag>-->
<!--                    </template>-->
<!--                </el-table-column>-->
                <el-table-column
                        align="center"
                        label="操作"
                        width="160"
                >
                    <template #default="scope">
<!--                        <el-button-->
<!--                                @click="handleUpdate(scope.row)"-->
<!--                                :link="true"-->
<!--                                style="color: #266fff"-->
<!--                        ><el-icon><Edit /></el-icon>更新-->
<!--                        </el-button>-->
                        <el-button
                                @click="keeps(scope.row)"
                                style="color: #3cb71c"
                                :link="true"
                        ><el-icon><Pointer /></el-icon>查看
                        </el-button>
                        <el-button
                                @click="handleDelete(scope.row)"
                                style="color: #FFB101"
                                :link="true"
                        ><el-icon><Delete /></el-icon>删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <template v-if="list.length!=0">
                <el-pagination
                        :page-sizes="[5, 10, 15, 20]"
                        :total="total"
                        @current-change="handleCurrentChange"
                        @size-change="handleSizeChange"
                        layout="total, sizes, prev, pager, next, jumper"
                        v-model:current-page="queryParams.pageNum"
                        v-model:page-size="queryParams.pageSize"
                />
            </template>
        </el-card>

        <el-drawer
                ref="drawerRef"
                v-model="dialog"
                title="药物方案制定"
                direction="rtl"
                class="demo-drawer"
        >
            <div style="padding: 20px 20px">
                <el-card class="box-card" style="text-align: left;line-height: 35px;margin: 20px 0px">
                    <h3><el-icon><Location /></el-icon>xx健康管理平台</h3>
                    <p style="color: #42b983"><el-icon><CircleCheck /></el-icon>时时刻刻做一个自律的人！</p>
                </el-card>

                <el-card class="box-card" style="text-align: left;line-height: 35px">
                    <el-form :model="form" ref="ruleFormRef" :rules="rules">
                        <el-row :gutter="20">
                            <el-col :span="24">
                                <el-form-item label="心情" prop="mood">
                                    <el-radio-group v-model="form.mood">
                                        <el-radio label="L" border>美丽至极</el-radio>
                                        <el-radio label="M" border>还不错</el-radio>
                                        <el-radio label="S" border>emo~了</el-radio>
                                    </el-radio-group>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row :gutter="20">
                            <el-col :span="12">
                                <el-form-item label="身高" prop="height">
                                    <el-input v-model="form.height">
                                        <template #append>cm</template>
                                    </el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="体重" prop="weight">
                                    <el-input v-model="form.weight">
                                        <template #append>斤</template>
                                    </el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row :gutter="20">
                            <el-col :span="12">
                                <el-form-item label="血压" prop="bloodPressure">
                                    <el-input v-model="form.bloodPressure">
                                        <template #append>mmHg</template>
                                    </el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="血糖" prop="bloodSugar">
                                    <el-input v-model="form.bloodSugar">
                                        <template #append>mmol/L</template>
                                    </el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row :gutter="20">
                            <el-col :span="24">
                                <el-form-item label="心率" prop="heartRate">
                                    <el-input v-model="form.heartRate">
                                        <template #append>次/分钟</template>
                                    </el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row :gutter="20">
                            <el-col :span="24">
                                <el-form-item label="饮食" prop="diet">
                                    <el-input
                                            v-model="form.diet"
                                            :rows="5"
                                            type="textarea"
                                            placeholder="250字以内！"
                                    />
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div style="padding: 10px 30px;position: relative;float: right;">
                        <el-button type="success" @click="submitForm">提交</el-button>
                    </div>
                </el-card>
            </div>
        </el-drawer>

        <el-drawer
                ref="drawerRef"
                v-model="dialog2"
                title="日志详情"
                direction="ltr"
                class="demo-drawer"
        >
            <div style="padding: 20px 20px">
                <el-card class="box-card" style="text-align: left;line-height: 35px;margin: 20px 0px">
                    <h3>体检预约报告</h3>
                    <p><el-icon><Location /></el-icon>xxx健康管理系统</p>
                    <p style="color: #42b983"><el-icon><CircleCheck /></el-icon>时时刻刻做一个自律的人！</p>
                </el-card>

                <el-card class="box-card" style="text-align: left;line-height: 35px">
                    <el-descriptions title="日志详情" column="2">
                        <el-descriptions-item label="记录时间">
                            {{ $filters.formatDate(detail.time) }}
                        </el-descriptions-item>
                        <el-descriptions-item label="身高">{{detail.height}} cm</el-descriptions-item>
                        <el-descriptions-item label="体重">{{detail.weight}} 斤</el-descriptions-item>
                        <el-descriptions-item label="心情">
                            <el-tag class="ml-2" type="warning" v-if="detail.mood==='L'">美丽至极</el-tag>
                            <el-tag class="ml-2" type="warning" v-if="detail.mood==='M'">还不错</el-tag>
                            <el-tag class="ml-2" type="warning" v-if="detail.mood==='S'">emo~了</el-tag>
                        </el-descriptions-item>
                        <el-descriptions-item label="睡眠">
                            <el-tag class="ml-2" type="warning" v-if="detail.mood==='L'">极好</el-tag>
                            <el-tag class="ml-2" type="warning" v-if="detail.mood==='M'">一般</el-tag>
                            <el-tag class="ml-2" type="warning" v-if="detail.mood==='S'">失眠</el-tag>
                        </el-descriptions-item>
                        <el-descriptions-item label="血糖">{{detail.bloodSugar}} mmol/L</el-descriptions-item>
                        <el-descriptions-item label="血压">{{detail.bloodPressure}} mmHg</el-descriptions-item>
                        <el-descriptions-item label="心率">{{detail.heartRate}} 次/分钟</el-descriptions-item>

                    </el-descriptions>
                    <div style="position: relative;float: right;padding: 10px 30px">
                        <el-button type="success" @click="detailCancel">关闭</el-button>
                    </div>
                </el-card>
            </div>
        </el-drawer>

    </div>
</template>

<script>

    export default {
        data() {
            return {
                archive:{},
                detail:{},
                dialog2:false,
                dialog:false,
                dialogFormVisible:false,
                // 遮罩层
                loading: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // 用户表格数据
                list: [],
                // 弹出层标题
                title: "",
                // 表单参数
                form: {},
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    beginTime: '',
                    endTime: '',
                },
                roleOptions:[
                    {
                        label:"普通用户",
                        value:"U",
                    },
                    {
                        label:"管理员",
                        value: "A",
                    }
                ],
                // 表单校验
                rules: {
                    mood: [
                        {required: true, message: "请选择你此时此刻的心情", trigger: "blur"},
                    ],
                    diet: [
                        {required: true, message: "请填写", trigger: "blur"},
                    ],
                    sleep: [
                        {required: true, message: "请填写", trigger: "blur"},
                    ],
                    height: [
                        {required: true, message: "请填写", trigger: "blur"},
                    ],
                    weight: [
                        {required: true, message: "请填写", trigger: "blur"},
                    ],
                    bloodPressure: [
                        {required: true, message: "请填写", trigger: "blur"},
                    ],
                    heartRate: [
                        {required: true, message: "请填写", trigger: "blur"},
                    ],
                    medicineName: [
                        {required: true, message: "请填写", trigger: "blur"},
                    ],
                }
            };
        },
        created() {

        },
        mounted(){
            this.getUserList();
        },
        methods: {
            // selectTime(value){
            //     let v=JSON.parse(JSON.stringify(value));
            //     console.log()
            //     console.log("---------------")
            //     console.log(v[0])
            //     this.queryParams.beginTime=v[0];
            //     this.queryParams.endTime=v[1];
            //     console.log(this.queryParams)
            // },
            detailCancel(){
                this.dialog2=false;
            },
            async getArchivesByNumber() {
                const {data: res} = await this.$http.get('archives/getArchivesByNumber', {
                    params: {
                        archiveNumber:this.form.archiveNumber,
                    }
                })
                if (res.code==200){
                    this.archive=res.data;
                }else{
                    this.$message.error("档案不存在！");
                }
            },
            keeps(rowData){
                // console.log("--------")
                // console.log(rowData)
                this.detail=rowData;
                this.dialog2=true;
            },

            addButton(){
                this.reset();
                this.dialog=true;
            },

            async getUserList() {
                console.log("-=-=-=-=")
                console.log(JSON.parse(JSON.stringify(this.queryParams)))
                this.loading = true;
                const {data: res} = await this.$http.get('record/getHealthRecords', {params:this.queryParams})
                console.log(res);
                this.list = res.data.children;
                this.total = res.data.total;
                this.loading = false;
            },
            // 取消按钮
            cancel() {
                this.dialog = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form = {
                    id:0,
                    time:'',//记录时间
                    mood:'L',//心情
                    diet:'',
                    sleep:'',
                    height:'',
                    weight:'',
                    bloodPressure:'',
                    heartRate:'',
                    bloodSugar:''
                };
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.getUserList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                // 查询参数
                this.queryParams = {
                    pageNum: 1,
                    pageSize: 10,
                    userPhone: "",
                },
                    this.getUserList();
            },
            /** 修改按钮操作 */
            async handleUpdate(row) {
                console.log(row)
                this.form = JSON.parse(JSON.stringify(row));
                this.dialog = true;
            },
            /** 提交按钮 */
            async submitForm() {
                await this.$refs["ruleFormRef"].validate(valid => {
                    if (valid) {
                        if (this.form.id != 0) {
                            this.modifyFrom();
                        } else {
                            this.addFromFun();
                        }
                    }
                });
                this.dialog = false;
            },
            /**修改用户*/
            async modifyFrom() {
                const {data: res} = await this.$http.post('medicine/updateHealthMedicine', this.form);
                if (res.code == 200) {
                    this.getUserList();
                    this.$message.success('更新成功!')
                } else {
                    this.$message.error('服务器异常')
                }
            },
            /**
             * 添加用户
             * */
            async addFromFun() {
                const {data: res} = await this.$http.post('record/insertHealthRecord', this.form);
                console.log(res)
                if (res.code === 200) {
                    this.$message.success('日志提交成功！')
                    this.getUserList();
                } else {
                    this.$message.error('服务器异常！')
                }
            },
            /** 删除按钮操作 */
            async handleDelete(row) {
                console.log(row)
                this.$confirm('是否确定删除日志?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(async () => {
                    const {data: res} = await this.$http.get('record/delHealthRecord', {params:{
                            id:row.id
                        }});
                    console.log(res)
                    if (res.code == 200) {
                        this.getUserList();
                        this.$message.success('已成功删除！')
                    } else {
                        this.$message.error('服务器异常！')
                    }
                }).catch(() => {
                    this.$message.info('已取消删除！')
                })
            },
            handleSizeChange(newSize) {
                this.queryParams.pageSize = newSize;
                this.getUserList();
            }
            ,
            handleCurrentChange(current) {
                this.queryParams.pageNum = current;
                this.getUserList();
            },
        }
    };
</script>
